/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.swirrel.installer;

import java.awt.Component;
import java.awt.event.MouseListener;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import org.swirrel.annotation.MouseClicked;
import org.swirrel.annotation.MouseEntered;
import org.swirrel.annotation.MouseExited;
import org.swirrel.annotation.MousePressed;
import org.swirrel.annotation.MouseReleased;
import org.swirrel.listener.SwirrelMouseListener;
import org.swirrel.util.MethodCall;

/**
 *
 * @author Gronau
 */
public class MouseListenerInstaller extends AbstractListenerInstaller implements SwirrelListenerInstaller {

    public MouseListenerInstaller() {
        super(Arrays.<Class<? extends Annotation>>asList(MouseClicked.class,
                MousePressed.class, MouseReleased.class,
                MouseEntered.class, MouseExited.class),
                Arrays.<Class<?>>asList(Component.class));
    }

    public boolean isListenerAlreadySet(Object obj) {
        for (MouseListener ml : ((Component) obj).getMouseListeners()) {
            if (ml instanceof SwirrelMouseListener) {
                return true;
            }
        }
        return false;
    }

    public void registerListener(Field field, Component comp, List<MethodCall> methodCalls) throws IllegalAccessException {
        ((Component) field.get(comp)).addMouseListener(
                new SwirrelMouseListener(methodCalls.get(0), methodCalls.get(1),
                methodCalls.get(2), methodCalls.get(3), methodCalls.get(4)));
    }
}
